package com.jiamao.goplay.ui.settings;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;

import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.SPUtils;

import com.github.iielse.switchbutton.SwitchView;
import com.gyf.immersionbar.ImmersionBar;
import com.jiamao.goplay.BR;
import com.jiamao.goplay.R;
import com.jiamao.goplay.app.AppViewModelFactory;
import com.jiamao.goplay.app.NimSDKOptionConfig;
import com.jiamao.goplay.base.BaseMVVMFragment;
import com.jiamao.goplay.config.Config;
import com.jiamao.goplay.config.preference.UserPreferences;
import com.jiamao.goplay.databinding.SettingsNewMsgRemindLayoutBinding;
import com.jiamao.goplay.ui.settings.vm.NewMessageRemindVM;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.StatusBarNotificationConfig;

public class NewMessageRemindFragment extends BaseMVVMFragment<SettingsNewMsgRemindLayoutBinding, NewMessageRemindVM> {
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.settings_new_msg_remind_layout;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }


    @Override
    public NewMessageRemindVM initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用LoginViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(NewMessageRemindVM.class);
    }

    @Override
    public void initData() {
        super.initData();
        ImmersionBar.with(this). keyboardEnable(false).statusBarDarkFont(true).titleBar(binding.getRoot().findViewById(R.id.toolbar)).init();
        TextView titleTv =  binding.getRoot().findViewById(R.id.tvTitle);
        titleTv.setText("新消息通知");
        binding.getRoot().findViewById(R.id.ll_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });
        KLog.d("aaaaaabbbbbbbbbbbbbbbbbbbbb:"+SPUtils.getInstance().getBoolean(Config.APP_SETINGS_TZ,true));

        binding.swTzs.setOpened(SPUtils.getInstance().getBoolean(Config.APP_SETINGS_TZ,true));
        binding.swSy.setOpened(SPUtils.getInstance().getBoolean(Config.APP_SETINGS_SY,true));
        binding.swZd.setOpened(SPUtils.getInstance().getBoolean(Config.APP_SETINGS_ZD,true));
        binding.swDetail.setOpened(SPUtils.getInstance().getBoolean(Config.APP_SETINGS_DETAIL,true));

        if(SPUtils.getInstance().getBoolean(Config.APP_SETINGS_TZ,true)){
            viewModel.visbe.set(View.VISIBLE);
        }else {
            viewModel.visbe.set(View.GONE);
        }

        binding.swTzs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isOpened =  binding.swTzs.isOpened();
                SPUtils.getInstance().put(Config.APP_SETINGS_TZ,isOpened);
                NIMClient.toggleNotification(isOpened);
                binding.swSy.setOpened(isOpened);
                binding.swZd.setOpened(isOpened);
                binding.swDetail.setOpened(isOpened);
                SPUtils.getInstance().put(Config.APP_SETINGS_SY,isOpened);
                SPUtils.getInstance().put(Config.APP_SETINGS_ZD,isOpened);
                SPUtils.getInstance().put(Config.APP_SETINGS_DETAIL,isOpened);
                StatusBarNotificationConfig config = UserPreferences.getStatusConfig();
                if (config == null){
                    config = NimSDKOptionConfig.loadStatusBarNotificationConfig();
                }
                config.ring = isOpened;
                config.vibrate = isOpened;
                config.hideContent = !isOpened;
                NIMClient.updateStatusBarNotificationConfig(config);
                UserPreferences.setStatusConfig(config);

                if (!isOpened){
                    viewModel.visbe.set(View.GONE);
                }
                else {
                    viewModel.visbe.set(View.VISIBLE);
                }




            }
        });
        binding.swSy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isOpened =  binding.swSy.isOpened();
                SPUtils.getInstance().put(Config.APP_SETINGS_SY,isOpened);

               // NIMClient.toggleNotification(isOpened);

                // 更新消息提醒配置 StatusBarNotificationConfig，以设置不响铃为例。
                StatusBarNotificationConfig config = UserPreferences.getStatusConfig();
                if (config == null){
                    config = NimSDKOptionConfig.loadStatusBarNotificationConfig();
                }
                config.ring = isOpened;
//                config.notificationExtraType = NotificationExtraTypeEnum.MESSAGE;
                NIMClient.updateStatusBarNotificationConfig(config);
                UserPreferences.setStatusConfig(config);
            }
        });
        binding.swZd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isOpened =  binding.swZd.isOpened();
                SPUtils.getInstance().put(Config.APP_SETINGS_ZD,isOpened);

                StatusBarNotificationConfig config = UserPreferences.getStatusConfig();
                if (config == null){
                    config = NimSDKOptionConfig.loadStatusBarNotificationConfig();
                }
                config.vibrate = isOpened;
//                config.notificationExtraType = NotificationExtraTypeEnum.MESSAGE;
                NIMClient.updateStatusBarNotificationConfig(config);
                UserPreferences.setStatusConfig(config);
            }
        });
        binding.swDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isOpened =  binding.swDetail.isOpened();
                SPUtils.getInstance().put(Config.APP_SETINGS_DETAIL,isOpened);
                StatusBarNotificationConfig config = UserPreferences.getStatusConfig();
                if (config == null){
                    config = NimSDKOptionConfig.loadStatusBarNotificationConfig();
                }
                config.hideContent = !isOpened;
//                config.notificationExtraType = NotificationExtraTypeEnum.MESSAGE;
                NIMClient.updateStatusBarNotificationConfig(config);
                UserPreferences.setStatusConfig(config);

            }
        });



    }
}